<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrderDetails extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('orderId')->default(0)->comment("kf_orders表id");
            $table->unsignedInteger('productId')->default(0)->comment("菜品 Id");
            $table->unsignedInteger('menuId')->default(0)->comment("菜单 Id");
            $table->unsignedInteger('categoryId')->default(0)->comment("菜品分类 Id");
            $table->unsignedInteger('num')->default(0)->comment("菜品数量");
            $table->string('unit', 30)->default('')->comment("菜品单位");
            $table->unsignedInteger('originalUnitPrice')->default(0)->comment("原单价");
            $table->unsignedInteger('originalPrice')->default(0)->comment("原总价");
            $table->unsignedInteger('shouldUnitPrice')->default(0)->comment("应付单价");
            $table->unsignedInteger('payPrice')->default(0)->comment("应付总价");
            $table->unsignedInteger('refundNum')->default(0)->comment("退菜数量");
            $table->unsignedInteger('freeNum')->default(0)->comment("赠菜数量");
            $table->unsignedInteger('discountType')->default(0)->comment("优惠类型 1折扣 2减免");
            $table->unsignedInteger('discountNum')->default(0)->comment("优惠值 90折, 5元等");
            $table->unsignedInteger('discountPrice')->default(0)->comment("优惠价格");
            $table->string('imageUrl', 255)->default(0)->comment('菜品图片');

            // 统一表字段
            $table->integer('createdAt')->default(0)->comment('创建时间');
            $table->integer('createdByUid')->default(0)->comment('创建人');
            $table->integer('updatedAt')->default(0)->comment('修改时间');
            $table->integer('updatedByUid')->default(0)->comment('修改人');
            $table->string('updatedBy')->default(0)->comment('修改人');
            $table->string('createdBy')->default(0)->comment('创建人');

            $table->tinyInteger('isDeleted')->default(0)->comment("是否删除 0：未删除，1：已删除");

            // 索引部分
            $table->index('orderId', 'idx_orderId');  // 索引
            $table->index('createdAt', 'idx_createdAt');  // 索引

            $table->collation = 'utf8mb4_general_ci';
        });

        $prefix = config('other.DB_PREFIX', '');
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$prefix}order_details` COMMENT = '订单明细表'"); //表注释
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_details');
    }
}
